package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;

/**
 *  青岛社区 解析类
 *
 * @author Chase
 *
 */
public class QingdaonewsBBSAnalyse implements AnalyseNews {

	

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://club.qingdaonews.com/showAnnounce.php[?]board_id=[0-9]*&topic_id=[0-9]*",
				"http://club.qingdaonews.com/showAnnounce.php[?]topic_id=[0-9]*&board_id=[0-9]*",
				"http://club.qingdaonews.com/showAnnounce_[0-9]*_[0-9]*_[1]_[0-9]*.htm",
				"http://yezhu.qingdaonews.com/forum.php\\?.*mod=viewthread&tid=[0-9]*",
				"http://yezhu.qingdaonews.com/thread-[0-9]*-[1]-[0-9]*.html"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String url = urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if (!isDetailPage(url)) {
			System.err.println("不符合规则");
		}

		String html = urlMeta.getHtml();

		/*
		 * 定义新闻实体需要的字段
		 */
		String title = null;
		String content = null;
		Integer commentNum = 0;
		Integer clickNum = 0;

		Document doc = Jsoup.parse(html);

		title = doc.select("h1").text();// 标题
		if(StringUtils.isBlank(title))
				title = doc.select("div.tit h2").text();// 标题

		content = HtmlCleaner.getContentHtml(url,  doc.select("div.text")); //内容
		if(StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url,  doc.select("td.t_f"));
		}

		String commTxt = doc.select("span.z").text();
		clickNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "人气："));
		commentNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "回复："));

    	String author = doc.select("div#maincontainer span#vuser_name0 a.username").text();
    	if(StringUtils.isBlank(author)){
    		author=doc.select("a.name").text();
    	}
    	if(StringUtils.isBlank(author)){
    		try {
				author=doc.select(".authi a[href^=http://yezhu.qingdaonews.com/home.php?mod=space&uid=]").first().text();
			} catch (Exception e) {
			}
    	}

    	Long date = DateUtils.matchDate(doc.select("div#maincontainer div.vtools div.z,div.article_foot").text());
    	if(date==null){
    		try {
				date = DateUtils.matchDate(doc.select("em[id^=authorposton]").first().text());
			} catch (Exception e) {
			}
    	}

    	NewsMeta meta = new NewsMeta();
    	meta.setAuthor(author);
		meta.setTitle(title);
		meta.setContent(content);
		meta.setUrl(url);
		meta.setType(4);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);
		meta.setDate(date);

		/**
		 * 解析用于更新的地址
		 */
		String updateUrl= url;
		meta.setUpdateUrl(updateUrl);
		return meta;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(StringUtils.isNotBlank(updateUrl)){
				UrlMeta urlMeta  = CrawlHTML.responseToURL(updateUrl);
				return parserHtml(urlMeta);
			}
		}
		return null;
	}


	

}
